{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3b8a31d3",
   "metadata": {},
   "source": [
    "--------------------\n",
    "* 2022/9/20\n",
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f4de290",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "> 1.有序的数据结构之列表\n",
    "\n",
    "> 2.用户输入\n",
    "\n",
    "> 3.关于格式化文本信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3642fe84",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_01_name = \"Marry\"\n",
    "\n",
    "# 如果想一个变量存放一个班同学的名字，该怎么存？\n",
    "# 处理数据会不会比较麻烦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "05c0a313",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Marry,Alex,周晓明\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = \"Marry,Alex,周晓明\"\n",
    "print(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3691ce65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Alex'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list[6:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a329a9dc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 我想知道这个名单有多少个人名？\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d6be11a",
   "metadata": {},
   "source": [
    "##  1.引入列表的概念\n",
    "\n",
    "* Lists might contain items of different types, but usually the items all have the same type.\n",
    "> 我们已经学习了数值和字符，list可以放数值和字符，但是通常要么一起放数值，要么一起放字符串\n",
    "> 总结一些通用的有序数据结构的方法\n",
    ">> 1.取值：通过索引值取出相应的内容\n",
    ">> 2.切片：通过索引值区间取出一些对应的内容\n",
    ">> 3.in 可以查看成员对象是否存在\n",
    "\n",
    "> 列表的一些方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f2e97820",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "print(a,type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "efa61b81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Marry', 'Alex', '周晓明'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "网三_name_list = [\"Marry\",\"Alex\",\"周晓明\"]\n",
    "print(网三_name_list,type(网三_name_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e815d87",
   "metadata": {},
   "source": [
    "### 1.1 (operation)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8adadf94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Marry'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出 网三_name_list 中第一位同学的名字\n",
    "网三_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2aee250b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看 网三_name_list 有多少同学\n",
    "len(网三_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "520ee0f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网三： Marry\n",
      "网三： Alex\n",
      "网三： 周晓明\n"
     ]
    }
   ],
   "source": [
    "for i in 网三_name_list:\n",
    "    print(\"网三：\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3efe3952",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in可以查询数据是否存在\n",
    "\"植草\" in 网三_name_list\n",
    "\"Marry\" in 网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0859fc90",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关于运算法则(in + 切片 + 取值 len(s) s.count)\n",
    "网三_name_list_转专业 = [\"xiaoming\",\"lihua\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7ae40df5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', '周晓明', 'xiaoming', 'lihua']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list + 网三_name_list_转专业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3f3c678c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一个list 可不可以出现 重复项？\n",
    "网一_age_list = [18,19,18,19,19,18,18]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "cc9e3891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 19, 18, 19, 19, 18, 18]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网一_age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c3833528",
   "metadata": {},
   "source": [
    "## 2.2 列表的方法(Function）\n",
    "\n",
    "> 1.增\n",
    "\n",
    "> 2.删\n",
    "\n",
    "> 3.改\n",
    "\n",
    "> 4.查\n",
    "\n",
    "* <font color=\"red\">运算法则的操作不会改变列表的数据本身"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0af52608",
   "metadata": {},
   "outputs": [],
   "source": [
    "### append 增加，增加元素到末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6a703030",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list = [\"Marry\",\"Alex\",\"周晓明\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "768d2f7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', '周晓明']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ac895c5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"卓柔\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9c7d2494",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', '周晓明', '卓柔']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d567da60",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.append(\"杨洋\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "bae42cb2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', 'Alex', '周晓明', '卓柔', '杨洋']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "40abd04b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# insert 增加元素到某个位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "f88278c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "c5e4917f",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.insert(1,\"李华\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "7560c177",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', 'Alex', '周晓明', '卓柔', '杨洋']"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25bc4bdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4f8d6da9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "74d25262",
   "metadata": {},
   "outputs": [],
   "source": [
    "网三_name_list.remove('Alex')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "6dbd0381",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '周晓明', '卓柔', '杨洋']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "c72214e4",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[1;32mIn [39]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43m网三_name_list\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremove\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mAlex\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "网三_name_list.remove('Alex')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "513724a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '周晓明', '卓柔', '杨洋']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# remove 经常和in一起使用，先检查成员变量是否存在，然后再remove\n",
    "if \"Alex\" in 网三_name_list:\n",
    "    网三_name_list.remove(\"Alex\")\n",
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "9dacea4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# pop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "6eb76561",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(网三_name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "fe544971",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '周晓明', '卓柔', '杨洋']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "b8504fc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'杨洋'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "42d2d8b4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '周晓明', '卓柔']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "8473ea62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'周晓明'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "b70d3d7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Marry', '李华', '卓柔']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "cabcced5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请移除19的元素\n",
    "age_list = [18,19,17,19,18,17,16,20,17,16,17,18,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "4887422b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.count(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "2d202245",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[18, 17, 18, 17, 16, 20, 17, 16, 17, 18]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路1：\n",
    "删除的数量 = age_list.count(19)\n",
    "for i in range(删除的数量):\n",
    "    if 19 in age_list:\n",
    "        age_list.remove(19)\n",
    "        age_list\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "fece8594",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list = [18,19,17,19,18,17,16,20,17,16,17,18,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "f8dbd673",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 19\n",
      "2 19\n",
      "10 19\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[18, 17, 18, 17, 16, 20, 17, 16, 17, 18]"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 思路2：pop(index).先决条件，知道值出现的位置\n",
    "for i,v in enumerate(age_list):\n",
    "    if v ==19:\n",
    "        print(i,v)\n",
    "        age_list.pop(i)\n",
    "age_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4e1e561",
   "metadata": {},
   "source": [
    "* 课后练习 P 68.81\n",
    "* 剩下的reserve，extend，clear，copy，sort尝试练习\n",
    "* 总结列表的方法，用表格的形式写在markdown上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c15f934",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
